2373. 矩阵中的局部最大值
为保证权益,题目请参考 2373. 矩阵中的局部最大值(From LeetCode).
解决方案1
Python
python
from typing import List
class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
n = len(grid)
ans: List[List[int]] = [[0 for j in range(n - 2)] for i in range(n - 2)]
for i in range(n - 2):
for j in range(n - 2):
ans[i][j] = max(
grid[i][j],
grid[i][j + 1],
grid[i][j + 2],
grid[i + 1][j],
grid[i + 1][j + 1],
grid[i + 1][j + 2],
grid[i + 2][j],
grid[i + 2][j + 1],
grid[i + 2][j + 2],
)
return ans
if __name__ == "__main__":
so = Solution()
print(so.largestLocal([[9, 9, 8, 1], [5, 6, 2, 6], [8, 2, 6, 4], [6, 2, 2, 2]]))
print(
so.largestLocal(
[
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 2, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
]
)
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40